﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Xinchen.DbUtils;
using Xinchen.SOA.Manager.Api.Contracts;
using Xinchen.Utils;

namespace Xinchen.SOA.Client
{
    public class ServiceClient : Singleton<ServiceClient>
    {

        IServiceServices BuildService()
        {
            var _service = ServiceBuilder.BuildService<IServiceServices>(ServiceConsts.ServiceUrl, ServiceConsts.ServiceServiceName);
            return _service;
        }

        public PageResult<ServiceInfo> GetServices(int administratorId, int page, int pageSize, IList<SqlFilter> filters)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                return _service.GetServices().Where(x => x.AdministratorId == administratorId).Where(filters).Page(page, pageSize);
            }
        }


        public ServiceActionInfo GetServiceAction(int id)
        {
            return GetServiceActions(false, id).FirstOrDefault();
        }

        /// <summary>
        /// 根据编号获取Actions
        /// </summary>
        /// <param name="excepted">是包含还是排除</param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<ServiceActionInfo> GetServiceActions(bool excepted, params int[] ids)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                if (excepted)
                {
                    return _service.GetServiceActions().Where(x => !ids.Contains(x.Id)).ToList();
                }
                return _service.GetServiceActions().Where(x => ids.Contains(x.Id)).ToList();
            }
        }

        public PageResult<ServiceActionInfo> GetServiceActionsExcepted(int page, int pageSize, int serviceId, params int[] ids)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                return _service.GetServiceActions().Where(x => x.ServiceId == serviceId && !ids.Contains(x.Id)).Page(page, pageSize);
            }
        }

        public PageResult<ServiceActionInfo> GetServiceActions(int page, int pageSize, int serviceId, IList<SqlFilter> filters)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                return _service.GetServiceActions().Where(x => x.ServiceId == serviceId).Where(filters).Page(page, pageSize);
            }
        }

        public ServiceInfo GetService(int id)
        {
            return GetServices(id).FirstOrDefault();
        }
        public IList<ServiceInfo> GetServices(params int[] ids)
        {
            return BuildService().GetServices().Where(x => ids.Contains(x.Id)).ToList();
        }
        public IList<ServiceInfo> GetServicesBesides(params int[] ids)
        {
            try
            {
                return BuildService().GetServices().Where(x => !ids.Contains(x.Id)).ToList();
            }
            catch (WebException we)
            {
                return null;
            }
        }


        public void UpdateService(UpdateServiceRequest request)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                var result = _service.UpdateService(request);
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
            }
        }

        public void UpdateServiceAction(UpdateServiceActionRequest request)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                var result = _service.UpdateServiceAction(request);
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
            }
        }

        public void AddService(AddServiceRequest addServiceRequest)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                var result = _service.AddService(addServiceRequest);
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
            }
        }

        public void RemoveServices(params int[] ids)
        {
            var service = BuildService();
            using (service as IDisposable)
            {
                var result = service.RemoveServices(new RemoveServiceRequest()
                {
                    Ids = ids
                });
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
            }
        }

        public void AddServiceAction(string name, int serviceId, string description)
        {
            var service = BuildService();
            using (service as IDisposable)
            {
                var result = service.AddServiceAction(new AddServiceActionRequest()
                {
                    Description = description,
                    ServiceId = serviceId,
                    Name = name
                });
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
            }
        }

        public void RemoveServiceActions(int id)
        {
            var service = BuildService();
            using (service as IDisposable)
            {
                var result = service.RemoveServiceActions(new RemoveServiceActionsRequest()
                {
                    Ids = new int[] { id }
                });
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
            }
        }
    }
}
